﻿using System.Collections.Generic;
using CNF.Common.Base.SimpleOrm.DataBaseProvidrer;
using CNF.Common.Base.SimpleOrm.Entities;
using PetaPoco;
using System.Data;
using System;

namespace CNF.Common.Base.SimpleOrm.Data
{
    public abstract class BaseDataService<DataKeyType, EntityType> : BaseViewDataService<DataKeyType, EntityType>, IDataService<DataKeyType, EntityType>
        where DataKeyType : struct
        where EntityType : BaseEnity<DataKeyType>, new()
    {
        protected BaseDataService(IDataBaseStoreage<Database> dataBaseStoreage, string dataBaseKey)
            : base(dataBaseStoreage, dataBaseKey)
        {
        }

        public void Insert(EntityType entity)
        {
            CurrentDatabase.Insert(entity);
        }

        public void Update(EntityType entity)
        {
            CurrentDatabase.Update(entity);
        }

        protected int Update(string updateSql, params object[] args)
        {
            return CurrentDatabase.Update(updateSql, args);
        }

        public int PartUpdate(object partUpdateObject, DataKeyType dataKey)
        {
            return CurrentDatabase.Update(this.GetDbObjectName(), this.GetPkName(), partUpdateObject, dataKey);
        }

        public int PartInsert(object partInsertObject)
        {
            return CurrentDatabase.Update(this.GetDbObjectName(), this.GetPkName(), partInsertObject);
        }

        protected int Update(Sql updateSql)
        {
            return CurrentDatabase.Update(updateSql);
        }

        public int Update(EntityType entity, IEnumerable<string> columns)
        {
            return CurrentDatabase.Update(entity, columns);
        }

        protected int Delete(string deleteCondition, params object[] args)
        {
            return CurrentDatabase.Delete<EntityType>(deleteCondition, args);
        }

        protected int Delete(Sql deleteSql)
        {
            return CurrentDatabase.Delete(deleteSql);
        }

        public void Save(EntityType poco)
        {
            CurrentDatabase.Save(poco);
        }

        public void DeleteById(DataKeyType dataKey)
        {
            CurrentDatabase.Delete<EntityType>(" where " + this.GetPkName() + " = @0", dataKey);
        }

        public void Delete(EntityType entity)
        {
            CurrentDatabase.Delete(entity);
        }

        protected int Execute(string sql, params object[] args)
        {
            return CurrentDatabase.Execute(sql, args);
        }

        protected int Execute(Sql sql)
        {
            return CurrentDatabase.Execute(sql);
        }

  
    }
}
